import {NestFactory} from '@nestjs/core';
import {AppModule} from './app.module';
import config from "./config";
import {DocumentBuilder, SwaggerModule} from "@nestjs/swagger";
import {HttpExceptionFilter} from "./common/filters/HttpExceptionFilter";
import {JwtConstant} from "./common/constant/JwtConstant";
import {MyDtoSerializePipe} from "./common/pipe/MyDtoSerializePipe";

async function bootstrap() {
  console.log('Mysql数据库配置：', config.mysql)
  const app = await NestFactory.create(AppModule);
  // swagger
  const swgConfig = new DocumentBuilder()
      .setTitle('阿燕仓库管理系统')
      .setDescription('The ayan-respo API description')
      .setVersion('1.0')
      .addTag('')
      .addBearerAuth({
        in: 'header',
        type: 'http',
        bearerFormat: 'JWT',
        scheme: 'bearer',
      })
      .build();
  const document = SwaggerModule.createDocument(app, swgConfig);
  SwaggerModule.setup('swagger', app, document);

  // 跨域
  app.enableCors(  {
      origin: true,
      methods: '*',
      allowedHeaders: ['Content-Type', 'Authorization'],
      credentials: true,
  });
  // 全局异常过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 全局身份信息拦截器
  app.useGlobalInterceptors(new JwtConstant());
  // 全局Dto数据序列化和反序列化
  app.useGlobalPipes(new MyDtoSerializePipe());

  await app.listen(config.port);
  console.log('Application is running on ', config.port);
}
bootstrap();
